﻿using System;
using System.Collections.Generic;
using System.Linq;
using Core.DAL;
using Core.Utils;
using System.Web.Security;

namespace Core.BOL
{
    public class SectionRepository : IDisposable
    {
        private ScoutNetDataContext context = null;

        #region Construtor
        public SectionRepository()
        {
            context = GeneralUtils.GetDataContext();
        }
        #endregion

        #region Methodes
        public string GetSectionNameBySystemName(string systemName)
        {
            string sectionName = (from n in context.tSite_Sections
                                  where n.SystemName == systemName
                                  select n.Section).SingleOrDefault();

            return sectionName;
        }

        public List<tSite_Section> GetAllSections()
        {
            return context.tSite_Sections.ToList();
        }

        public int GetSectionID(string systemName)
        {
            int sectionName = (from n in context.tSite_Sections
                               where n.SystemName == systemName.ToLower()
                               select n.ID).SingleOrDefault();

            return sectionName;
        }

        public List<tSite_Section> GetSections()
        {
            List<tSite_Section> query = (from s in context.tSite_Sections
                                         orderby s.AnimationSection descending
                                         select s).ToList();

            return query;
        }

        public List<tSite_Section> GetNonAdministrativeSections()
        {
            var query = (from s in context.tSite_Sections
                         where s.AdministrativeSection == false
                         select s).ToList();

            return query;
        }

        public bool IsAdministrativeSection(string systemName)
        {
            bool query = (from s in context.tSite_Sections
                          where s.SystemName == systemName
                          select s.AdministrativeSection).SingleOrDefault();
            return query;
        }

        public tSite_Section GetSectionsById(int sectionID)
        {
            var section = (from s in context.tSite_Sections
                           where s.ID == sectionID
                           select s).SingleOrDefault();

            return section;
        }

        public tSite_Section GetSectionsBySystemName(string systemName)
        {
            var section = (from n in context.tSite_Sections
                           where n.SystemName == systemName
                           select n).SingleOrDefault();

            return section;
        }

        public List<tSite_Section> GetUserSectionsByUserId(Guid UID)
        {
            var lSections = (from s in context.tSite_Sections
                             join us in context.tSite_UsersSections on s.ID equals us.SID
                             where us.UID == UID
                             select s).ToList();
            return lSections;
        }

        public tSite_Section GetUserSectionByUserAndRole()
        {
            string[] roles = Roles.GetRolesForUser();
            tSite_Section section = null;

            if (roles[0].ToLower() == "c.u")
            {
                section = (from s in context.tSite_Sections
                           where s.ID == 7
                           select s).SingleOrDefault();
            }
            else
            {
                MembershipUser mu = Membership.GetUser();
                section = (from s in context.tSite_Sections
                           join up in context.tSite_UserProfiles on s.ID equals up.SectionID
                           where up.UserId == (Guid)mu.ProviderUserKey
                           select s).SingleOrDefault();
            }

            return section;
        }

        public void Dispose()
        {
            context.Dispose();
        }
        #endregion
    }
}
